﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Adams.Admin.Business.Pages
{
    public static class Create
    {
        public static Adams.Admin.DataTransfer.Pages.Create.PageLoad PageLoad(int id)
        {
            return DataAccess.Pages.Create.PageLoad(id);
        }

        public static DataTransfer.FormOutput Save(DataTransfer.Pages.Create.FormInput input)
        {
            var output = Validation.PerformBasicValidation(input);
            
            if (output.IsValid)
            {
                var pageinfo = DataAccess.Pages.Create.GetPageInfo(input.Url, input.ID.ToInt());

                if (input.ID.ToInt() > 0)
                {
                    if (!Shared.Str.IsEqual(input.Url, pageinfo.Url))
                    {
                        if (pageinfo.Exists)
                        {
                            output.InvalidFields.Add(new DataTransfer.InvalidField()
                            {
                                Name = "Url",
                                Code = 4
                            });
                        }
                    }
                }
                else
                {
                    if (pageinfo.Exists)
                    {
                        output.InvalidFields.Add(new DataTransfer.InvalidField()
                        {
                            Name = "Url",
                            Code = 4
                        });
                    }
                }

                if (output.IsValid)
                {
                    input.Url = "/" + input.Url.Trim('/');
                    DataAccess.Pages.Create.Save(input);
                    output.IsSaved = true;
                }
            }

            return output;
        }
    }
}
